//layui模板
layui.use(['layer', 'form', 'element', 'table', 'laypage'], function() {
	var layer = layui.layer,
		form = layui.form,
		element = layui.element,
		table = layui.table,
		laypage = layui.laypage;

	//查询banner
	var tableArray = new Array();
	AjaxPostFn('system/banner/select', "{}", function(jsonData) {
		if(jsonData.htmlState.status == 200) {
			if(jsonData.dataResultList.length != 0) {
				$.each(jsonData.dataResultList, function(idx, obj) {
					tableArray.push({
						"id": obj.id,
						"operation": " <a class='layui-btn ' lay-event='selectThis'  name='files'>修改图片</a><a class='layui-btn layui-bg-red ' lay-event='DelThis'>删除</a>",
						"albumId": obj.albumId,
						"albumName": obj.albumName,
						"name": obj.name,
						"linkurl": obj.linkurl,
						"cdnurl": obj.cdnurl,
						"imageUrl": obj.imageUrl,
						"imageUrlsee": "<img src='" + IMGUrl + '' + obj.imageUrl + "' alt='图片浏览' class='classifyimg'>",
						"imageNetUrl": obj.imageNetUrl,
						"ext": obj.ext,
						"setBanner": obj.setBanner,
						"width": obj.width,
						"height": obj.height,
						"status": obj.status,
						"isComment": obj.isComment,
						"isRecommend": obj.isRecommend,
						"sort": obj.sort,
						"remark": obj.remark,
						"publishTime": obj.publishTime,
						"publishUser": obj.publishUser,
						"updateTime": obj.updateTime,
						"updateId": obj.updateId,
						"createTime": obj.createTime,
						"sortNum": obj.sortNum,
						"createId": obj.createId
					});

				})
			}
		}
		var tableIns = table.render({
			elem: '#cctable',
			cols: [
				[ //标题栏
					{
						type: 'numbers',
						fixed: 'left',
						title: '行号'
					}, {
						field: 'id',
						minWidth: '0%',
						width: '0%',
						type: 'space',
						style: 'display:none'
					}, {
						field: 'operation',
						width: 200,
						title: '操作',
						fixed: 'left'
					}, {
						field: 'name',
						width: 150,
						title: '图片名称 ' + modDot,
						edit: 'text'
					}, {
						field: 'linkurl',
						width: 300,
						title: '链接地址' + modDot,
						edit: 'text'
					}, {
						field: 'imageUrlsee',
						width: 100,
						title: '图片预览'
					}, {
						field: 'sort',
						width: 100,
						title: '排序' + modDot,
						edit: 'text'
					}, {
						field: 'remark',
						width: 200,
						title: '备注' + modDot,
						edit: 'text'
					}, {
						field: 'updateTime',
						width: 200,
						title: '更新时间'
					}, {
						field: 'updateId',
						width: 100,
						title: '更新者'
					}, {
						field: 'createTime',
						width: 200,
						title: '创建时间'
					}, {
						field: 'createId',
						width: 100,
						title: '创建者'
					}
					//					, {
					//						field: 'status',
					//						width: 100,
					//						title: '状态',
					//						templet: '#checkboxTpl',
					//						unresize: true,
					//						fixed: 'right'
					//					}
					//					, {
					//						field: 'isComment',
					//						width: 100,
					//						title: '是否评论',
					//						templet: '#checkboxTpl3',
					//						unresize: true,
					//						fixed: 'right'
					//					}
					//					, {
					//						field: 'isRecommend',
					//						width: 100,
					//						title: '是否推荐',
					//						templet: '#checkboxTpl2',
					//						unresize: true,
					//						fixed: 'right'
					//					}
				]
			],
			data: tableArray,
			id: 'cctableid',
			size: 'lg',
			height: 'full-250',
			even: true,
			page: true, //是否显示分页
			done: function() {
				//图片点击放大
				TopimgFn();
			}
		}); //数据渲染结束
	})

	//监听单元格编辑
	table.on('edit(table100)', function(obj) {
		var value = obj.value //得到修改后的值
			,
			data = obj.data //得到所在行所有键值
			,
			field = obj.field; //得到字段
		//修改当前行的banner数据
		AjaxPostFn('system/banner/update', JSON.stringify(data), function(jsonData) {
			if(jsonData.htmlState.status == 200) {
				layer.msg("修改成功");
			}
		})
	});

	//是否banner的开关
	form.on('checkbox(lockDemo0)', function(obj) {
		var setBanner;
		obj.elem.checked ? setBanner = 1 : setBanner = 0;
		PdetUpdatebyidFn("{'id':'" + this.value + "','setBanner':'" + setBanner + "'}");
	});

	//是否显示的开关
	form.on('checkbox(lockDemo)', function(obj) {
		var status;
		obj.elem.checked ? status = 1 : status = 2;
		PdetUpdatebyidFn("{'id':'" + this.value + "','status':'" + status + "'}");
	});

	//是否评论的开关
	form.on('checkbox(lock2Demo)', function(obj) {
		var isComment;
		obj.elem.checked ? isComment = 1 : isComment = 0;
		PdetUpdatebyidFn("{'id':'" + this.value + "','isComment':'" + isComment + "'}")
	});

	//是否推荐的开关
	form.on('checkbox(lock3Demo)', function(obj) {
		var isRecommend;
		obj.elem.checked ? isRecommend = 1 : isRecommend = 0;
		PdetUpdatebyidFn("{'id':'" + this.value + "','isRecommend':'" + isRecommend + "'}")
	});

	//监听表格内的按钮事件
	table.on('tool(table100)', function(obj) {
		var data = obj.data;
		if(obj.event === 'DelThis') {
			//删除
			layer.confirm('是否删除该条分类信息', function(index) {
				AjaxPostFn('system/banner/deletebyid', "{'id':'" + data.id + "'}", function(jsonData) {
					if(jsonData.htmlState.status == 200) {
						layer.msg("删除成功");
					} else {

					}
				})
				obj.del();
				layer.close(index);

			});
		} else if(obj.event === 'selectThis') {
			//上传图片
			layer.open({
				title: '上传图片',
				area: ['390px', '260px'],
				shade: 0,
				maxmin: true,
				btn: ['上传'],
				content: ' <form action="'+AllUrl+'api/qiniu/update" method="post"  enctype="multipart/form-data" target="framFile"  class="updataform"  >  <input type="file" name="files"  multiple="true" class="file_upload" />  </form>',
				yes: function() {
					thisid = data.id;
					$(".updataform").submit();
				},
				success: function(layero) {}
			});
			var thisid = 1;
			var framFile = document.getElementById('framFile');
			var imgdata;
			framFile.onload = function() {
				imgdata = framFile.contentWindow.document.getElementsByTagName("pre")[0].innerHTML;
				imgdata = JSON.parse(imgdata).dataResultObj[0];
				AjaxPostFn('system/banner/update', "{'id':'" + thisid + "','imageUrl':'" + imgdata + "'}", function(jsonData) {
					if(jsonData.htmlState.status == 200) {
						layer.msg("修改成功");
						layer.closeAll();
						location.reload();
					}
				})
			};
		}
	});

	//点击弹出框的点击事件处理
	$('.openwinbtn').on('click', function() {
		var othis = $(this),
			method = othis.data('method');
		active[method] ? active[method].call(this, othis) : '';
	});

	var active = {
		//   弹出框事件
		addmang: function() {
			//新增
			layer.open({
				type: 1,
				skin: 'layui-layer-molv',
				title: '新增广告banner',
				area: ['500px', '600px'],
				content: $("#addBox").html(),
				skin: 'demo-class',
				btn: ["确认新增", "关闭"],
				yes: function(index, layero) {
					if(imgdata == undefined) {
						layer.msg("请选择图片上传");
					} else {
						var thisdata = $("#addform").serializeJson();
						thisdata.imageUrl = imgdata;
						AjaxPostFn('system/banner/insert', JSON.stringify(thisdata), function(jsonData) {
							if(jsonData.htmlState.status == 200) {
								layer.msg("增加成功");
								location.reload();
							}
						})
					}
				},
				success: function() {
					//form初始化
					form.render();
					$(".upNewimg").val("点击上传");
					
					
					$(".updataform1").attr("action",AllUrl+"api/qiniu/update");
					$(".updataform1").submit();
					//图片上传form的submit
					$(".upNewimg").click(function() {
							$(".updataform1").submit();

						})
						//图片上传的frame的窗口
					var framFile1 = document.getElementById('framFile1');
					framFile1.onload = function() {
						console.log("最新窗口点击");
						imgdata = framFile1.contentWindow.document.getElementsByTagName("pre")[0].innerHTML;
						imgdata = JSON.parse(imgdata).dataResultObj[0];
						console.log(imgdata);
						if(imgdata == null) {
							$(".upNewimg").val("点击上传");

						} else {
							$(".upNewimg").val("上传成功");
						}
					}

				}
			})
		}
	}
});

function PdetUpdatebyidFn(data) {
	//修改banner
	AjaxPostFn('system/banner/update', data, function(jsonData) {
		console.log(jsonData)
		if(jsonData.htmlState.status == 200) {
			layer.msg("修改成功");

		}

	});

}